<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: ***.php 5341 2010-09-07 09:04:47Z huuphuoc $
 * @since		2.0.5
 */

class ***_Models_Dao_Pdo_Mysql_*** extends Tomato_Model_Dao
	implements ***_Models_Interface_***
{
	public function convert($entity) 
	{
		return new ***_Models_***($entity); 
	}
	
	public function find($offset,$count,$exp=null){
		$select = $this->_conn
					->select()
					->from(array('c' => $this->_prefix . '+++'));
		if ($exp) {
            if (isset($exp['>>>'])) {
				$select->where("c.>>>=?",$exp[>>>]);
			}
		}		
        $select->order('c.id DESC');
        if(null != $count){
            $select->limit($count, $offset);
        }
        $rs = $select->query()
                     ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);		
	}
	
	
	public 	function count($exp){
		$select = $this->_conn
                        ->select()
                        ->from(array('c' => $this->_prefix . '+++'), array('num_+++' => 'COUNT(*)'));
        if ($exp) {
            if (isset($exp['>>>'])) {
                $select->where("c.>>>=?",$exp[>>>]);
            }
        }
        return $select->query()->fetch()->num_+++;	
	}	
	
	public function getById($id) 
	{
		$row = $this->_conn
					->select()
					->from(array('c' => $this->_prefix . '+++'))
					->where('c.id = ?', $id)
					->limit(1)
					->query()
					->fetch();
		return (null == $row) ? null : new ***_Models_***($row);
	}

	public function add($+++) 
	{
			$data = array(
				'name'		   => $+++->name,
				>>>	
			);
			
			if (isset($+++->id) && $+++->id != null) {
				$data['id'] = $+++->id;
				$this->_conn->insert($this->_prefix . '+++', $data);
				return $+++->id;				
			} else {
				$this->_conn->insert($this->_prefix . '+++', $data);
				return $this->_conn->lastInsertId($this->_prefix . '+++');
			}
		}
	}
	
	public function update($+++) 
	{
		$this->_conn->update($this->_prefix . '+++', 
							array(
								'name' 			=> $+++->name,
								>>>	
							), 
							array(
								'id = ?' => $+++->id,
							));
	}
	
	public function delete($+++) 
	{
		$this->_conn->delete($this->_prefix . '+++', 
							array(
								'id = ?' => $+++->id,
							));
	}	
}